/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.core;

import com.google.common.collect.Maps;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * 享元模式
 * 必传参数，子类定义；动态参数，写入map
 *
 * @author Coulson
 * @since 2025/3/26
 */
public abstract class Store<T> {

    private final Map<String, T> store;

    public Store() {
        this(null);
    }

    public Store(Map<String, T> store) {
        if (CollectionUtils.isEmpty(store)) {
            store = Maps.newHashMap();
        }
        this.store = store;
    }

    public void add(String key, T value) {
        store.put(key, value);
    }

    public void add(Map<String, T> params) {
        if (null == params || params.isEmpty()) {
            return;
        }
        this.store.putAll(params);
    }

    public Map<String, T> getStore() {
        return this.store;
    }

    public T get(String key) {
        return this.store.get(key);
    }

    public boolean contains(String key) {
        return this.store.containsKey(key);
    }

    public void remove(String key) {
        this.store.remove(key);
    }

    public void remove(List<String> keys) {
        for (String key : keys) {
            this.store.remove(key);
        }
    }

    public void clear() {
        this.store.clear();
    }

}
